﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.Web.Controllers
{
    public class ShoppingController : Controller
    {
        // GET: Shopping
        public ActionResult Index()
        {
            string Name = User.Identity.Name;

            if (Name == string.Empty)
            {
                return Redirect("/Login/Index");
            }
            else
            {
                var NameId = CustomerService.GetCustomer(Name);
                int Id = NameId.Id;
                ViewBag.b = NameId.Id;
                List<Shopping> list = ShoppingService.GetShoppings(Id);
                return View(list);
            }
        }
        /// <summary>
        /// 提交订单页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Cart(string Productid,string Count)
        {
            string Name = User.Identity.Name;

            if (Name == string.Empty)
            {
                return Redirect("/Login/Index");
            }
            else
            {
                var NameId = CustomerService.GetCustomer(Name);
                int Id = NameId.Id;
                ViewBag.b = NameId.Id;
                List<Address> list = AddressService.GetAddresses(Id);
                var ProductId = Convert.ToInt32(Productid);
                ViewBag.a = OrderDetailService.GetOrderDetailMsg(Productid);
                ViewBag.c = Count;
                ViewBag.d = Name;
                return View(list);
            }
         
        }
        public ActionResult Cart1()
        {
            string Name = User.Identity.Name;

            if (Name == string.Empty)
            {
                return Redirect("/Login/Index");
            }
            else
            {
                var NameId = CustomerService.GetCustomer(Name);
                int Id = NameId.Id;
                ViewBag.b = NameId.Id;
                List<Address> list = AddressService.GetAddresses(Id);
                ViewBag.a = ShoppingService.GetShoppings(Id);
                return View(list);
            }
        }
        [HttpPost]
        public ActionResult Delete(int Id)
        {
            int a = ShoppingService.Delete(Id);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "删除失败" });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult AddOrder(Order order)
        {
            int a = OrderService.AddOrderMsg(order);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "添加成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "添加失败" });
            }
        }


    }
}